package eip.chapter8.expiration.impl;

import org.mule.routing.outbound.FilteringOutboundRouter;
import org.mule.umo.UMOMessage;
import org.mule.umo.UMOSession;
import org.mule.umo.routing.RoutingException;

public class ExpirationRouter extends FilteringOutboundRouter {

	public final static String EIP_TIMESTAMP = "EIP_TIMESTAMP";
	public final static String EIP_TTL = "EIP_TTL";
	
	private int ttl;

	/**
	 * We just add a timestamp and a ttl to the outgoing message. This 
	 * timestamp and ttl can be checked when a message is received to 
	 * check how much time it spent in transit.
	 */
	@Override
	public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) throws RoutingException {
		message.setIntProperty(EIP_TTL,ttl);
		message.setLongProperty(EIP_TIMESTAMP,System.currentTimeMillis());
		return super.route(message, session, synchronous);
	}
	
	public int getTtl() {
		return ttl;
	}

	public void setTtl(int ttl) {
		this.ttl = ttl;
	}
}
